package Q388

import java.util.*
import kotlin.collections.ArrayDeque

class Main {

}

fun main() {

}

class Solution2 {
    fun lengthLongestPath(input: String): Int {
        val n = input.length
        var pos = 0
        var ans = 0
        val stack: Deque<Int> = java.util.ArrayDeque()
        while (pos < n) {
            /* 检测当前文件的深度 */
            var depth = 1
            while (pos < n && input[pos] == '\t') {
                pos++
                depth++
            }
            /* 统计当前文件名的长度 */
            var isFile = false
            var len = 0
            while (pos < n && input[pos] != '\n') {
                if (input[pos] == '.') {
                    isFile = true
                }
                len++
                pos++
            }
            /* 跳过当前的换行符 */pos++
            while (stack.size >= depth) {
                stack.pop()
            }
            if (!stack.isEmpty()) {
                len += stack.peek() + 1
            }
            if (isFile) {
                ans = Math.max(ans, len)
            } else {
                stack.push(len)
            }
        }
        return ans
    }
}
